$(document).ready(function(){
	$(".alert").alert('close');
	$('#add_customer_from').validate({
	    rules: {
	      cusName: {
	        minlength: 3,
	        required: true
	      },
	      cusPwd: {
	    	minlength: 6,
	    	maxlength: 12,
	        required: true
	      },
	      confirmpwd: {
	    	  minlength: 6,
		   	  maxlength: 12,
		      required: true
	      },
	      cusRealName: {
	    	  minlength: 2,
		      required: true
	      },
	    },
		highlight: function(element) {
			$(element).closest('.control-group').removeClass('success').addClass('error');
		},
		success: function(element) {
			element.text('').addClass('valid').closest('.control-group').removeClass('error').addClass('success');
		},
		submitHandler: function(form) {
			if($('#cusPwd').val()!=$('#confirmpwd').val()){
				$('#errormsg').html("两次密码不一致");
	    		$('.alert1').show(500);
	    		return;
			}
			$.ajax({
			    type: 'POST',
			    url: server+"admin/customer/customer_add" ,
			    data: $("#add_customer_from").serialize() ,
			    success: function(data){
			    	if(data.status=="success"){
			    		$('.alert1').hide(500);
			    		$('#cusId').val("");
			    		$('#cusName').val("");
			    		$('#cusPwd').val("");
			    		$('#cusRealName').val("");
			    		$('#confirmpwd').val("");
			    		$('#operatName').html("添加客服");
			    		$('#saveOrUpdate').html("添加");
			    		$('#myModal').modal('hide');
			    		window.location.href=server+"admin/customer/customer_list";
			    	}else{
			    		$('#errormsg').html(data.msg);
			    		$('.alert1').show(500);
			    	}
			    } ,
			    error:	function(data){
			    	$('#errormsg').html("添加失败!请重新登录");
		    		$('.alert1').show(500);
			    },
			    dataType: "JSON"
			});
		}
  });
});

/**
 * 跳转更新页面
 * @param id
 */
function toUpdate(id)
{
	$('#operatName').html("修改客服");
	$('#saveOrUpdate').html("修改");
	$.ajax({
	    type: 'POST',
	    url: server+"admin/customer/customer_one" ,
	    data: {"id":id} ,
	    success: function(data){
	    	if(data.status=="success"){
	    		$('#myModal').modal('show');
	    		$('.alert1').hide(500);
	    		$('#cusId').val(data.id);
	    		$('#cusName').val(data.cusName);
	    		$('#cusPwd').val(data.cusPwd);
	    		$('#confirmpwd').val(data.cusPwd);
	    		$('#cusRealName').val(data.cusRealName);
	    	}
	    } ,
	    error:	function(data){
	    	$('#errormsg').html("加载数据失败!请重新登录");
    		$('.alert1').show(500);
	    },
	    dataType: "JSON"
	});
}

/**
 * 关闭弹出层
 */
function closeMyModal()
{
	$('.alert1').hide(500);
	$('#cusId').val("");
	$('#cusName').val("");
	$('#cusPwd').val("");
	$('#cusRealName').val("");
	$('#confirmpwd').val("");
	$('#operatName').html("添加客服");
	$('#saveOrUpdate').html("添加");
	$('#myModal').modal('hide');
}

function add(){
	$('#myModal').modal('show');
}

function del(id){
	bootbox.dialog("确定删除吗?", [{
	    "label" : "取消"
	},{
	    "label" : "确定",
	    "class" : "btn-success",
	    "callback": function() {
	    	$.ajax({
	    	    type: 'GET',
	    	    url: server+"admin/customer/del/"+id ,
	    	    success: function(data){
	    	    	if(data.status=="success"){
	    	    		window.location.href=server+"admin/customer/customer_list";
	    	    	}else{
	    	    		bootbox.alert(data.msg);
	    	    	}
	    	    } ,
	    	    error:	function(data){
	    	    	bootbox.alert("删除失败");
	    	    },
	    	    dataType: "JSON"
	    	});
	    }
	} 
	]);
}